<script setup>
import { ref } from 'vue';
import { Edit, Delete } from '@element-plus/icons-vue'

const dialogVisible = ref(false)
const title = ref('');

//数据(返回到的数据给他)
const categorys = ref([
    {
        categoryName: 'java',
        categoryDes: '添砖java',
        createTime: '2020-01-01 20:01:01'
    }
])

//存放数据模型（对象）
const categoryModel = ref({
    categoryName: '',
    categoryDes: '',
    categoryTime: ''
})

//添加分类表单校验
const rules = {
    categoryName: [
        { required: true, message: '请输入分类名称', trigger: 'blur' },
    ],
    categoryDes: [
        { required: true, message: '请输入分类别名', trigger: 'blur' },
    ]
}

import { CategoryListService, addCategoryService, updateCategoryService, deleteCategoryService } from '@/api/category';
import { ElMessage, ElMessageBox } from 'element-plus';

const showCategoryList = async () => {
    let result = await CategoryListService();
    categorys.value = result.data;
}

showCategoryList();

//添加分类
const addCategroy = async () => {
    let result = await addCategoryService(categoryModel.value);

    ElMessage.success('添加成功')
    showCategoryList();
    dialogVisible.value = false;
}

//点击添加按钮之后，清空里面的数据
const clear = () => {
    categoryModel.value = {
        categoryName: '',
        categoryDes: ''
    }
}

//显示编辑弹窗
const showUpdateDialog = (row) => {
    title.value = '编辑分类';
    dialogVisible.value = true;
    categoryModel.value.categoryName = row.categoryName;
    categoryModel.value.categoryDes = row.categoryDes;
    categoryModel.value.id = row.id;
}

//编辑分类
const updateCategory = async () => {
    let result = await updateCategoryService(categoryModel.value);
    ElMessage.success(result.message ? result.message : '修改成功')
    showCategoryList();
    dialogVisible.value = false;

}

//删除分类
const deleteCategory = (row) => {
    
    ElMessageBox.confirm(
        '你确定要删除该分类吗？',
        '温馨提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            let result = await deleteCategoryService(row.id);
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
            showCategoryList();
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}

</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>文章分类</span>
                <div class="extra">
                    <el-button type="primary" @click="dialogVisible = true; clear(); title = '添加分类'">添加分类</el-button>
                </div>
            </div>
        </template>
        <el-table :data="categorys" style="width: 100%">
            <el-table-column label="序号" width="100" type="index"> </el-table-column>
            <el-table-column label="分类名称" prop="categoryName"></el-table-column>
            <el-table-column label="分类描述" prop="categoryDes"></el-table-column>
            <el-table-column label="创建时间" prop="createTime"></el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showUpdateDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteCategory(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>

        <!-- 添加分类弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title" width="30%">
            <el-form :model="categoryModel" :rules="rules" label-width="100px" style="padding-right: 30px">
                <el-form-item label="分类名称" prop="categoryName">
                    <el-input v-model="categoryModel.categoryName" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item label="分类描述" prop="categoryDes">
                    <el-input v-model="categoryModel.categoryDes" minlength="1" maxlength="30"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="title == '添加分类' ? addCategroy() : updateCategory()"> 确认
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>